package com.kbatis.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 高亮
 */
public class HighLightUtil {

    public static String contentHighLight(String content, String prefix, String posfix) {
        Pattern pattern = Pattern.compile(prefix);
        StringBuffer buf = new StringBuffer();
        Matcher findMatcher = pattern.matcher(content);
        int number = 0;
        while(findMatcher.find()) {
            number++;
            if(number % 2 == 1){//奇数
                findMatcher.appendReplacement(buf, "<span style=\"color:red\">");
            } else {
                findMatcher.appendReplacement(buf, "</span>");
            }
        }
        findMatcher.appendTail(buf);
        String s = buf.toString();
        if (s.contains(posfix)) {
            s = s.replaceAll("!!!", "</span>");
        }
        return s;
    }
}
